﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace 视频流媒体管理
{
    public partial class use_api : System.Web.UI.Page
    {
        public static string token = "";
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn登录_Click(object sender, EventArgs e)
        {
            //  string pwd = Sea.Common.Encrypt.MD5.生成MD5(this.txt口令.Text.Trim().ToLower()); //口令用md5加密，小写
            string url = "http://" + this.Request.Url.Authority + "/api_v1.ashx?method=login";

            JObject ob = new JObject();
            ob["username"] = this.txt用户id.Text.Trim();
            ob["password"] = this.txt口令.Text.Trim();
            ob["onlytoken"] = "true";

            视频流媒体管理.Code.HttpPost p = new Code.HttpPost();
            string str = p.PostData(url, "application/json", ob.ToString(), new Dictionary<string, string>());
            //{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzAyNjcyMDksInB3IjoiMjEyMzJmMjk3YTU3YTVhNzQzODk0YTBlNGE4MDFmYzMiLCJ0bSI6MTczMDE4MDgwOSwidW4iOiJhZG1pbiJ9.a0ok1e7ohUbd5_2mdDOrsYJ3owelMxA4MKVfZOye_Bg"}
            //Newtonsoft.Json.Bson.j
            if (str.Contains("token") == false)
            {
                this.txtInfo.Text = "登录失败：" + str;
                token = "";
            }
            else
            {
                this.txtInfo.Text = "登录成功：" + str;
                JObject t = JObject.Parse(str);
                token = t["token"].ToString();
            }
        }

        protected void btn退出登录_Click(object sender, EventArgs e)
        {
            string url = "http://" + this.Request.Url.Authority + "/api_v1.ashx?method=logout";            //https://blog.csdn.net/qq_51860400/article/details/132455026

            JObject ob = new JObject();
            ob["token"] = token;

            视频流媒体管理.    Code.HttpPost p = new   Code.HttpPost();
            string str = p.PostData(url, "application/json", ob.ToString(), new Dictionary<string, string>());
            //{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzAyNjcyMDksInB3IjoiMjEyMzJmMjk3YTU3YTVhNzQzODk0YTBlNGE4MDFmYzMiLCJ0bSI6MTczMDE4MDgwOSwidW4iOiJhZG1pbiJ9.a0ok1e7ohUbd5_2mdDOrsYJ3owelMxA4MKVfZOye_Bg"}
            //Newtonsoft.Json.Bson.j
            this.txtInfo.Text = "登录退出：" + str;
        }

        protected void btn取得直播列表_Click(object sender, EventArgs e)
        {
            string url = "http://" + this.Request.Url.Authority + "/api_v1.ashx?method=live_list";            //https://blog.csdn.net/qq_51860400/article/details/132455026

            JObject ob = new JObject();
            //ob["token"] = token;

            视频流媒体管理.Code.HttpPost p = new Code.HttpPost();
            string str = p.PostData(url, "application/json", ob.ToString(), new Dictionary<string, string>());

            DataTable dt = Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(str);
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
        }

        protected void btn取得直播列表0_Click(object sender, EventArgs e)
        {
            string url = "http://" + this.Request.Url.Authority + "/api_v1.ashx?method=live_get";            //https://blog.csdn.net/qq_51860400/article/details/132455026

            JObject ob = new JObject();
            //ob["token"] = token;
            ob["id"] = this.txt直接ID.Text;

            视频流媒体管理.Code.HttpPost p = new Code.HttpPost();
            string str = p.PostData(url, "application/json", ob.ToString(), new Dictionary<string, string>());

            DataTable dt = Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(str);
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
        }

        protected void btn打开关闭直播_Click(object sender, EventArgs e)
        {

            string url = "http://" + this.Request.Url.Authority + "/api_v1.ashx?method=turn_actived";            //https://blog.csdn.net/qq_51860400/article/details/132455026

            JObject ob = new JObject();
            //ob["token"] = token;
            ob["id"] = this.txt直接ID.Text;
            ob["actived"] = this.txt直播开头.Text;

            视频流媒体管理.Code.HttpPost p = new Code.HttpPost();
            string str = p.PostData(url, "application/json", ob.ToString(), new Dictionary<string, string>());
            this.txtInfo.Text = "打开关闭：：" + str; //{"code":200,"msg":"Success"}
        }

        protected void btn新增直播_Click(object sender, EventArgs e)
        {
            string url = "http://" + this.Request.Url.Authority + "/api_v1.ashx?method=live_save";

            JObject ob = new JObject();
           // ob["token"] = token;
            ob["cid"] = this.txt摄像机ID.Text;
            ob["name"] = this.txt摄像机名.Text;
            ob["vo[type]"] = "online";
            ob["vo[src]"] = this.txt直接源.Text;
            ob["kind"] = "pull";
            ob["actived"] = "true"; //添加后开始直播，也可后续再控制

            视频流媒体管理.Code.HttpPost p = new Code.HttpPost();
            //string cons = p.组合FormData(dict, out string contType);
            string str = p.PostData(url, "", ob.ToString(), new Dictionary<string, string>());
            //{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzAyNjcyMDksInB3IjoiMjEyMzJmMjk3YTU3YTVhNzQzODk0YTBlNGE4MDFmYzMiLCJ0bSI6MTczMDE4MDgwOSwidW4iOiJhZG1pbiJ9.a0ok1e7ohUbd5_2mdDOrsYJ3owelMxA4MKVfZOye_Bg"}
            //Newtonsoft.Json.Bson.j

            this.txtInfo.Text = "新增直播：" + str; //{"code":200,"msg":"Success"}
        }

        protected void btn删除直播_Click(object sender, EventArgs e)
        {
            string url = "http://" + this.Request.Url.Authority + "/api_v1.ashx?method=live_remove";

            JObject ob = new JObject();
           // ob["token"] = token;
            ob["id"] = this.txt摄像机ID.Text;

            视频流媒体管理.Code.HttpPost p = new   Code.HttpPost();
            string str = p.PostData(url, "application/json", ob.ToString(), new Dictionary<string, string>());
            this.txtInfo.Text = "删除信息：" + str;

        }

        protected void btn新增直播xls_Click(object sender, EventArgs e)
        {
            //从xls中取摄像机
            string url = "http://" + this.Request.Url.Authority + "/api_v1.ashx?method=xls_live";

            JObject ob = new JObject();
            // ob["token"] = token;
            ob["id"] = this.txt摄像机ID.Text;

            视频流媒体管理.Code.HttpPost p = new Code.HttpPost();
            string str = p.PostData(url, "application/json", ob.ToString(), new Dictionary<string, string>());
            this.txtInfo.Text = "删除信息：" + str;
        }
    }
}